Imports TaxonNamesInterface


Public Class NameSearch
    Implements INavigationControl

    Public MaxResults As Integer = 201
    Public Event NavigateToEvent(ByVal sender As UserControl, ByVal request As NavigationRequest) Implements INavigationControl.NavigateToEvent
    Public Event MergeNames()

    Public SelectedMergeNames As List(Of TaxonNamesInterface.Name)


    Private Sub NameSearchRecords_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Tag = "Search names"
        ResultsGrid.AutoGenerateColumns = False
        FilterText.Focus()
    End Sub


    Private Sub DoSearch()
        Windows.Forms.Cursor.Current = Cursors.WaitCursor
        Try
            lblMsg.Text = ""
            Dim names As List(Of TaxonNamesInterface.Name) = NamesClient.NamesInterfaceProxy.ListNameRecords(FilterText.Text.Trim, anywhereCheck.Checked) 'todo, MaxResults)
            ResultsGrid.DataSource = names

            If names.Count > 0 Then
                For Each row As DataGridViewRow In ResultsGrid.Rows
                    Dim nm As Name = row.DataBoundItem
                    If nm.NameRank IsNot Nothing Then
                        row.Cells(RankCol.Name).Value = nm.NameRank.Name
                    End If
                Next

                If names.Count > MaxResults - 1 Then
                    lblMsg.Text = "> " + MaxResults.ToString + " names found"
                Else
                    lblMsg.Text = (names.Count - 1).ToString + " names found"
                End If
            Else
                lblMsg.Text = "No names found"
            End If
        Catch ex As Exception
            NamesException.LogError(ex)
            NamesClient.NamesInterfaceProxy = Nothing
            MsgBox("Search Error")
        End Try
        Windows.Forms.Cursor.Current = Cursors.Default
    End Sub

    Private Sub ListButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListButton.Click
        DoSearch()
    End Sub


    Private Function GetSelectedNames() As Integer
        Dim count As Integer = 0

        SelectedMergeNames = Nothing

        For Each r As DataGridViewRow In ResultsGrid.Rows
            If r.Cells("Selected").Value IsNot DBNull.Value AndAlso r.Cells("Selected").Value Then
                count += 1

                If SelectedMergeNames Is Nothing Then SelectedMergeNames = New List(Of TaxonNamesInterface.Name)
                SelectedMergeNames.Add(NamesClient.NamesInterfaceProxy.GetName(r.Cells("NameGuid").Value))
            End If
        Next

        Return count
    End Function


    Private Sub ResultsGrid_CellBeginEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles ResultsGrid.CellBeginEdit
        If e.ColumnIndex <> 0 Then e.Cancel = True
    End Sub

    Private Sub MergeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MergeButton.Click
        Try
            Dim count As Integer = GetSelectedNames()
            If count = 0 Then Return

            If MsgBox("Are you sure you want to merge all selected provider names into one consensus name?", MsgBoxStyle.YesNo, "Merge") = MsgBoxResult.Yes Then
                'select consensus name
                RaiseEvent MergeNames()
                DoSearch()
            End If
        Catch ex As Exception
            NamesException.LogError(ex)
            NamesClient.NamesInterfaceProxy = Nothing
        End Try
    End Sub


    Private Sub ProvNamesGrid_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs)
        If e.ColumnIndex <> 0 Then e.Cancel = True
    End Sub

    Private Sub FilterText_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles FilterText.KeyPress
        If e.KeyChar = Chr(13) Then
            DoSearch()
        End If
    End Sub

    Private Sub ResultsGrid_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles ResultsGrid.CellContentClick
        'do edit?
        Try
            If ResultsGrid.Columns(e.ColumnIndex).Name = EditCol.Name Then
                Dim req As New NavigationRequest
                req.Type = NavigationType.EditName
                req.Id = ResultsGrid.Rows(e.RowIndex).Cells(IdCol.Name).Value.ToString
                RaiseEvent NavigateToEvent(Me, req)
            End If
        Catch ex As Exception
            NamesException.LogError(ex)
        End Try
    End Sub

End Class
